//
//  BSRegistView.m
//  SuoSiApp
//
//  Created by zhang on 16/4/5.
//  Copyright © 2016年 Ym. All rights reserved.
//

#import "BSRegistView.h"

@interface BSRegistView ()<UITextFieldDelegate>

@end

@implementation BSRegistView

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
       [self addSubviews] ;
    }
    return self;
}

- (void)addSubviews{

    self.backgroundImageview = [[UIImageView alloc]initWithFrame:self.bounds];
    self.backgroundImageview.userInteractionEnabled = YES;
    self.backgroundImageview.image = [UIImage imageNamed:@"register_bg.jpg"];

    [self addSubview:_backgroundImageview];

    self.iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kScrenWidth /3 - 20, kMartin50, kScrenWidth/3 + kMartin20 , kScrenWidth/6 + 20)];
    self.iconImageView.image = [UIImage imageNamed:@"register_com_top.png"];
    [self addSubview:_iconImageView];

    NSInteger kmartin = 0;
    if (kScrenWidth > 370) {
        kmartin = 70;
    } else {
        kmartin = 50;
    }
    
//        contentView
    self.contentView1 = [[UIView alloc]initWithFrame:CGRectMake(kScrenWidth / 18, self.iconImageView.bottom + kMartin20, kScrenWidth * 8 / 9, kScrenWidth/9 * 7)];
    [self addSubview:_contentView1];
    
    
//    账号注册
  CGRect frame1 =  CGRectMake(0, 0, self.contentView1.width, kScrenWidth/9);
    UIColor *color = [UIColor colorWithRed:200.0/255 green:200.0/255 blue:200.0/255 alpha:1];
   self.accountRegistLB = [self makeLable:frame1 :color :@"账号注册"];
    self.accountRegistLB.textAlignment = NSTextAlignmentCenter;
//    账号
    CGRect frame2 =  CGRectMake(0, self.accountRegistLB.bottom, kmartin, kScrenWidth/9);
    self.accountLB = [self makeLable:frame2 :BSWhite :@"账号:"];
//    输入账号
    CGRect frame3 = CGRectMake(self.accountLB.right, self.accountLB.top, self.contentView1.width - self.accountLB.width, self.accountLB.height);
    self.accountTF = [self makeTextFeild:frame3 :@"请输入手机号"];
    self.accountTF.tag = 500;
    
    self.accountTF.keyboardType = UIKeyboardTypeNumberPad;

//    获取验证码
    CGRect frame4 = CGRectMake(0, self.accountTF.bottom, self.contentView1.width, kScrenWidth/9);
    self.blankLB = [self makeLable:frame4 :BSWhite :nil];
    
    self.getVerificatBT = BSButton;
    self.getVerificatBT.frame = CGRectMake(self.accountTF.left, self.accountTF.bottom + 3, kScrenWidth / 4, self.accountLB.height - 6);
    [self.getVerificatBT setBackgroundImage:[UIImage imageNamed:@"register_gettoken.png"] forState:(UIControlStateNormal)];
    [self.contentView1 addSubview:self.getVerificatBT];
    
    self.timeLB = [[UILabel alloc]initWithFrame:CGRectMake(self.getVerificatBT.right, self.getVerificatBT.top + kMartin5, kMartin30, self.getVerificatBT.height - kMartin10)];
//    self.timeLB.backgroundColor = BSGreen;
    self.timeLB.textColor = BSRed;
    self.timeLB.text = @"60s";
    [self.contentView1 addSubview:self.timeLB];
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(self.timeLB.right + kMartin5, self.timeLB.top, kScrenWidth - self.timeLB.right - kMartin5, self.timeLB.height)];
    lable.text = @"后,重新发送";
    [self.contentView1 addSubview:lable];
//    验证码
    CGRect frame5 = CGRectMake(0, self.blankLB.bottom, kmartin + kMartin20, self.accountLB.height);
    self.verificationLB = [self makeLable:frame5 :BSWhite :@"验证码:"];
//    输入验证码
    CGRect frame6 = CGRectMake(self.verificationLB.right, self.verificationLB.top, self.contentView1.width - self.verificationLB.width, self.verificationLB.height);
    self.verificationTF = [self makeTextFeild:frame6 :@"请输入验证码"];
    self.verificationTF.keyboardType = UIKeyboardTypeNumberPad;
    self.verificationTF.tag = 504;
//    密码
    CGRect frame7 = CGRectMake(0, self.verificationLB.bottom, kmartin, self.verificationLB.height);
    self.passwordLB = [self makeLable:frame7 :BSWhite :@"密码:"];
//    输入密码
    CGRect frame8 = CGRectMake(self.passwordLB.right, self.passwordLB.top, self.contentView1.width - self.passwordLB.width, self.passwordLB.height);
    self.passwordTF = [self makeTextFeild:frame8 :@"请输入密码"];
    self.passwordTF.secureTextEntry = YES;
    self.passwordTF.tag = 501;
//    确认密码
    CGRect frame9 = CGRectMake(0, self.passwordLB.bottom, kmartin + kMartin40, self.passwordLB.height);
    self.confirmPasswordLB = [self makeLable:frame9 :BSWhite :@"确认密码:"];
    
    CGRect frame10 = CGRectMake(self.confirmPasswordLB.right, self.confirmPasswordLB.top, self.contentView1.width - self.confirmPasswordLB.width, self.confirmPasswordLB.height);
    self.confirmPasswordTF = [self makeTextFeild:frame10 :@"请再次输入您的密码"];
    self.confirmPasswordTF.secureTextEntry = YES;
        self.confirmPasswordTF.tag = 501;
//    身份证
    CGRect frame11 = CGRectMake(0, self.confirmPasswordLB.bottom, kmartin + kMartin20, self.confirmPasswordLB.height);
   self.IDNumberLB = [self makeLable:frame11 :BSWhite :@"身份证:"];
//    输入身份证
    CGRect frame12 = CGRectMake(self.IDNumberLB.right, self.IDNumberLB.top, self.contentView1.width - self.IDNumberLB.width, self.IDNumberLB.height);
    
    self.IDTF = [self makeTextFeild:frame12 :@"请输入您的身份证号"];
    self.IDTF.tag  =506;
//    self.IDTF.delegate = nil;
//    同意用户协议书
    
//    UILabel *protocolLB = [[UILabel alloc]initWithFrame:CGRectMake(kScrenWidth / 2 - kMartin50 - kMartin40, self.contentView1.bottom +kMartin20, kMartin100 + kMartin80, kMartin20)];
//    protocolLB.text  = @"同意《用户协议书》";
//    [self.backgroundImageview addSubview:protocolLB];
    
    self.protocolBT = BSButton;
    _protocolBT.frame = CGRectMake(kScrenWidth / 2 - kMartin50 - kMartin40, self.contentView1.bottom +kMartin20, kMartin100 + kMartin80, kMartin20);
    [_protocolBT setTitle:@"同意《用户协议书》" forState:(UIControlStateNormal)];
    [_protocolBT setTitleColor:BSBlack forState:(UIControlStateNormal)];
    
    [self.backgroundImageview addSubview:_protocolBT];

    
    
//    立即注册
    self.registNowBT = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.registNowBT.layer.cornerRadius = 5;
    self.registNowBT.frame = CGRectMake(self.contentView1.left, _protocolBT.bottom + kMartin10, self.contentView1.width, self.IDNumberLB.height);
    [self.registNowBT setTitle:@"立即注册" forState:(UIControlStateNormal)];
//    [self.registNowBT setBackgroundImage:[UIImage imageNamed:@"buttonbg.png"] forState:(UIControlStateNormal)];
    self.registNowBT.backgroundColor = BSSaveBTGreen;
    _registNowBT.enabled = NO;
    [self.backgroundImageview addSubview:_registNowBT];
    
//    同意按钮
    self.agreenBT = BSButton;
    self.agreenBT.frame = CGRectMake(_protocolBT.left - _protocolBT.height - kMartin5, _protocolBT.top, _protocolBT.height, _protocolBT.height);
    [self.agreenBT setImage:[UIImage imageNamed:@"register_notSelectProtoco.png"] forState:(UIControlStateNormal)];
    [self.backgroundImageview addSubview:_agreenBT];
    
   
//    添加边框
//    CGRect fram1 = CGRectMake(self.accountRegistLB.left, self.accountRegistLB.bottom, self.accountRegistLB.width, 1);
    CGFloat y = self.accountRegistLB.bottom;
    for (int i = 0; i < 7; i++) {
        [self addImageView:y :@"zhzc_17.png"];
        y = y + self.accountLB.height;

    }

    
    
    

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (UILabel *)makeLable:(CGRect)frame :(UIColor *)color : (NSString *)text{

    UILabel *lable = [[UILabel alloc]initWithFrame:frame];
   // lable.backgroundColor = [UIColor colorWithRed:178.0/255 green:178.0/255 blue:178.0/255 alpha:1];
    lable.backgroundColor = color;
    lable.textAlignment = NSTextAlignmentLeft;
    if (text) {
        lable.text = [NSString stringWithFormat:@"  %@",text];;

    }
    if (kScrenWidth > 370) {
         lable.font = [UIFont systemFontOfSize:18];
    } else {
         lable.font = [UIFont systemFontOfSize:16];
    }
   
    [self.contentView1 addSubview:lable];
    return lable;
}

- (UITextField *)makeTextFeild:(CGRect)frame :(NSString *)placeHolder{
    
    UITextField *textFeild = [[UITextField alloc]initWithFrame:frame];
    // lable.backgroundColor = [UIColor colorWithRed:178.0/255 green:178.0/255 blue:178.0/255 alpha:1];
    textFeild.delegate  = self;
    textFeild.placeholder = placeHolder;
    if (kScrenWidth > 370) {
        textFeild.font = [UIFont systemFontOfSize:18];
    } else {
        textFeild.font = [UIFont systemFontOfSize:16];
    }

    textFeild.clearButtonMode = UITextFieldViewModeWhileEditing;
    textFeild.returnKeyType = UIReturnKeyDone;
    textFeild.backgroundColor = BSWhite;
    [self.contentView1 addSubview:textFeild];
    return textFeild;
}

- (UIImageView *)addImageView:(CGFloat)y :(NSString *)name{

        CGRect frame = CGRectMake(0, y, self.contentView1.width, 1);
    
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:frame];
    imageview.image = [UIImage imageNamed:name];
    [self.contentView1 addSubview:imageview];
    return imageview;

}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    
    
    UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
    CGPoint pp = [self convertPoint:CGPointMake(0, textField.bottom) toView:currentWindow];
    
    
    self.keyboardOffset = self.contentView1.top + pp.y + 253 - kScrenHeight ;
    
    NSLog(@"%f",self.contentView1.top);
//     NSLog(@"%f",self.contentView1.top);
    
    
    if (253 + pp.y + self.contentView1.top + kMartin20> kScrenHeight ) {
        NSLog(@"%f",self.keybordHeight);
        
        CGFloat duration = .3;
        
        [UIView animateWithDuration:duration animations:^{
            
            self.transform = CGAffineTransformTranslate(self.transform, 0, -self.keyboardOffset - kMartin20);
            self.IschangekeyBoard = YES;
            
        }];
    } ;
}




/*
self.verificationTF.tag = 504;
注册的密码
self.passwordTF.tag = 505;
注册的身份证
self.IDTF.tag  =506;
***/



@end
